# _*_ coding: utf-8 _*_
from openerp import models, fields, api


class ProductParameter(models.Model):
    _name = "product.parameter"
    _rec_name = "prd_no"
    _description = "Product Parameter"
    _order = "prd_no"

    prd_no = fields.Char(string="prd_no", index=True)
    filepage = fields.Integer(string="File Page")
    fileprefix = fields.Char(string="File Prefix")
    jpgfile = fields.Char(string="jpg File")
    spec1 = fields.Char(string="spec1",help='方头尺寸接口尺寸-扭力扳手，输入方寸-扭力放大器')
    spec2 = fields.Char(string="spec2" ,help='公制扭矩范围-扭力扳手，，输出方寸-扭力放大器')
    spec3 = fields.Char(string="spec3" ,help='英制扭矩范围-扭力扳手，英制输入扭力-扭力放大器')
    spec4 = fields.Char(string="spec4",help='增量-扭力扳手，英制输出扭力-扭力放大器')
    spec5 = fields.Char(string="spec5",help='制式-扭力扳手，公制输入扭力-扭力放大器')
    spec6 = fields.Char(string="spec6",help='精度-扭力扳手，扭力精度比-扭力放大器')
    spec7 = fields.Char(string="spec7",help='长度-扭力扳手，力臂长度-扭力放大器')
    spec8 = fields.Char(string="spec8",help='重量-扭力扳手，重量-扭力放大器')
    spec9 = fields.Char(string="spec9",help='头部宽度-扭力扳手，宽度-扭力放大器')
    spec10 = fields.Char(string="spec10",help='头部厚度-扭力扳手，高度-扭力放大器')
    spec11 = fields.Char(string="spec11",help='头部样式-扭力扳手，公制输出扭力-扭力放大器')
    spec12 = fields.Char(string="spec12",help='棘轮齿数-扭力扳手，扭矩比-扭力放大器')
    spec13 = fields.Char(string="spec13",help='棘轮最小工作角度-扭力扳手，齿轮比-扭力放大器')
    spec14 = fields.Char(string="spec14",help='包装盒型号-扭力扳手，工具盒-扭力放大器')
    spec15 = fields.Char(string="spec15",help='棘轮头配件型号-扭力扳手，替换方头-扭力放大器')
    spec16 = fields.Char(string="spec16",help='模式-扭力扳手，量角器-扭力放大器')
    spec17 = fields.Char(string="spec17",help='延长杆-扭力扳手，反力臂重-扭力放大器')
    spec18 = fields.Char(string="spec18",help='角度调整范围-扭力扳手，损失-扭力放大器')
    spec19 = fields.Char(string="spec19",help='预置型-扭力扳手')
    spec20 = fields.Char(string="spec20",help='接口类型-扭力扳手，反作用机构类型-扭力放大器')
    spec21 = fields.Char(string="spec21")
    spec22 = fields.Char(string="spec22")
    spec23 = fields.Char(string="spec23",help='外壳颜色-扭力扳手')
    spec24 = fields.Char(string="spec24",help='输入方寸-扭力扳手')
    spec25 = fields.Char(string="spec25",help='')
    subdomain = fields.Char(string="Sub Domain")
    page = fields.Char(string="Page")
    mrk = fields.Char(string="Marks")
    desc = fields.Char(string="Desc")

    product_id = fields.Many2one("product.template", "Product", ondelete="restrict")
    url = fields.Char(string="URL", compute="_compute_url",store=True,readonly=False)

    @api.depends("subdomain","fileprefix","page")
    @api.multi
    def _compute_url(self):
        for parameter in self:
            if parameter.fileprefix and parameter.subdomain and parameter.filepage:
                url = "http://%(subdomain)s.findtool.cn/%(fileprefix)s%(fpage)s.php?resultKey=%(prd_no)s" % {
                    "subdomain": parameter.subdomain,
                    "fileprefix": parameter.fileprefix,
                    "fpage": str(parameter.filepage),
                    "prd_no": parameter.product_id.default_code
                }
            else:
                url=""
            parameter.url = url

    # 以下为了更新product.parameter url字段
    @api.multi
    def put_url(self):
        paras=self.env['product.parameter']
        recs=paras.search([])
        for rec in recs:
            if rec.fileprefix and rec.subdomain and rec.filepage:
                url = "http://%(subdomain)s.findtool.cn/%(fileprefix)s%(fpage)s.asp?resultKey=%(prd_no)s" % {
                    "subdomain": rec.subdomain,
                    "fileprefix": rec.fileprefix,
                    "fpage": str(rec.filepage),
                    "prd_no": rec.product_id.default_code

                }
            else:
                url=""
            rec.url = url


